const { DataTypes, Op } = require("sequelize");
const { sequelize } = require("../config/database");

const TokenTransaction = sequelize.define(
  "TokenTransaction",
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_uuid: {
      type: DataTypes.UUID,
      allowNull: false,
      comment: "用户UUID",
    },
    type: {
      type: DataTypes.ENUM(
        "purchase",
        "subscription_grant",
        "consumption",
        "refund",
        "reserve",
        "confirm",
        "cancel",
        "promotional_grant",
        "compensation_grant"
      ),
      allowNull: false,
      comment:
        "交易类型：购买、订阅赠送、消费、退款、预扣、确认、取消、活动赠送、补偿发放",
    },
    amount: {
      type: DataTypes.DECIMAL(10, 3), // 修改为DECIMAL支持小数，总长度10位，小数3位
      allowNull: false,
      comment: "代币数量（正数为增加，负数为减少）",
    },
    balance_after: {
      type: DataTypes.DECIMAL(10, 3), // 修改为DECIMAL支持小数，总长度10位，小数3位
      allowNull: false,
      comment: "交易后的余额",
    },
    payment_order_id: {
      type: DataTypes.UUID,
      allowNull: true,
      comment: "关联的支付订单ID",
    },
    description: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "交易描述",
    },
    // 新增字段：预扣相关
    reserve_id: {
      type: DataTypes.UUID,
      allowNull: true,
      comment: "预扣ID，用于关联预扣、确认、取消操作",
    },
    status: {
      type: DataTypes.ENUM("pending", "confirmed", "canceled"),
      allowNull: false,
      defaultValue: "confirmed",
      comment: "交易状态：待确认、已确认、已取消",
    },
    expires_at: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: "预扣过期时间",
    },
    related_service: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "关联的业务服务（如：ai_chat, image_generation）",
    },
    request_id: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "请求ID，用于幂等性控制",
    },
    // 代币来源类型（用于成本核算）
    token_source: {
      type: DataTypes.ENUM("purchased", "promotional", "compensated"),
      allowNull: true,
      comment: "代币来源：购买、活动、补偿（仅用于增加代币的交易）",
    },
    // 扩展数据（JSON格式，存储扣除详情等）
    metadata: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: "扩展数据，JSON格式（如：扣除详情、退还信息等）",
    },
    // 服务类型（用于预扣代币时记录具体的服务类型）
    server_type: {
      type: DataTypes.ENUM(
        "text",
        "img",
        "voice",
        "video",
        "subscription",
        "disposable",
        "promotional"
      ),
      allowNull: true,
      comment:
        "服务类型：文本生成、图片生成、声音生成、视频生成、订阅、一次性购买、活动赠送",
    },
  },
  {
    tableName: "token_transactions",
    comment: "代币交易记录表",
    indexes: [
      {
        unique: true,
        fields: ["reserve_id"],
        where: {
          reserve_id: { [Op.ne]: null },
        },
      },
      {
        unique: true,
        fields: ["request_id"],
        where: {
          request_id: { [Op.ne]: null },
        },
      },
      {
        fields: ["user_uuid", "status"],
      },
      {
        fields: ["expires_at"],
      },
    ],
  }
);

module.exports = TokenTransaction;
